//Base class for client side progress bar.
function ProgressBar(i_minValue, i_maxValue, i_step, i_numElements, i_hostElement, i_elmtColor, i_barID)
{
	this.minValue = i_minValue;
	this.maxValue = i_maxValue;
	this.numIncrements = i_numElements;
	this.progressColor = i_elmtColor;
	this.barID = i_barID;
	
	
	//Find the correct step size
	var range = this.maxValue-this.minValue
	
	if(range < 0)
		range*= -1;
	
	var numSteps = Math.round(range/i_step)
	if(numSteps == 0)
		numSteps = 1;
	
	this.currStep = this.numIncrements/numSteps
	
	//Render the bar itself inside the host element	
	this.BuildBar(i_hostElement)
}

//The number of progress increments that make up the progress bar
ProgressBar.prototype.numIncrements = 9;
//The color of the progress elements
ProgressBar.prototype.progressColor = 'blue';
//Where is the bar now
ProgressBar.prototype.progressAt = 0; //none selected
//The minimum value displayed in the bar
ProgressBar.prototype.minValue = 0; //none selected
//The maximum value displaued in the bar
ProgressBar.prototype.maxValue = 0; //none selected
//What is one step increment
ProgressBar.prototype.currStep = 0; //none selected
//ID of the bar if more than one bar are present on the same page
ProgressBar.prototype.barID = 0; 

//Builds the skeleton of the progress bar
ProgressBar.prototype.BuildBar = ProgressBarrBuildBar; 
function ProgressBarrBuildBar(i_hostElement)
{
	var output = "<div style='font-size:8pt;padding:2px;border:solid black 1px'>"
	for(var i = 1; i <= this.numIncrements; i++)
	{
		output += "<span id='ProgElmt#" + this.barID + "_" + i + "'>&nbsp;&nbsp;&nbsp;</span>\n"
	}
	output += "</div><div align='center' id='numDisplay#" + this.barID + "'></div>";
	
	if(document.getElementById(i_hostElement) == null)
	{
		alert("Unable to render the progress bar because the host element " + i_hostElement + " was not found!");
		return;
	}
	document.getElementById(i_hostElement).innerHTML = output;
}

//Highlights a number of elements in the bar
ProgressBar.prototype.UpdateProgress = ProgressBarUpdateProgress
function ProgressBarUpdateProgress(i_numElmts)
{
	this.progressAt = i_numElmts;
	if (this.progressAt > this.numIncrements) 
		this.progressAt = this.numIncrements;
	
	for(var i = 1; i <= this.numIncrements; i++)
	{
		if(i <= Math.ceil(this.progressAt))
	 		document.getElementById('ProgElmt#'+ this.barID + "_" + i).style.backgroundColor = this.progressColor;
	 	else
	 		document.getElementById('ProgElmt#'+ this.barID + "_" + i).style.backgroundColor = 'transparent';
	}
	
	document.getElementById('numDisplay#' + this.barID).innerHTML = Math.round((this.progressAt/this.numIncrements)*100) + ' %'
}

//makes the progress bar show certain percent
ProgressBar.prototype.SetPercent = ProgressBarSetPercent
function ProgressBarSetPercent(i_percent)
{
	if(i_percent > 100 || i_percent < 0)
		return;
		
	this.UpdateProgress(Math.round(this.numIncrements * i_percent / 100));	
}

//Makes the bar increase by one step
ProgressBar.prototype.Step = ProgressBarStep
function ProgressBarStep()
{
	this.UpdateProgress(this.progressAt + this.currStep);
}